package com.uking.udppush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

public class HomeKeyObserver {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "HomeKeyObserver":"HomeKeyObserver";
    private Context mContext;
    private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver;
    private IntentFilter mIntentFilter;
    private OnHomeKeyListener mOnHomeKeyListener;

    class HomeKeyBroadcastReceiver extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

        HomeKeyBroadcastReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.intent.action.CLOSE_SYSTEM_DIALOGS")) {
                String reason = intent.getStringExtra("reason");
                if (reason != null && HomeKeyObserver.this.mOnHomeKeyListener != null) {
                    if (reason.equals("homekey")) {
                        HomeKeyObserver.this.mOnHomeKeyListener.onHomeKeyPressed();
                    } else if (reason.equals("recentapps")) {
                        HomeKeyObserver.this.mOnHomeKeyListener.onHomeKeyLongPressed();
                    }
                }
            }
        }
    }

    public interface OnHomeKeyListener {
        void onHomeKeyLongPressed();

        void onHomeKeyPressed();
    }

    public HomeKeyObserver(Context context) {
        this.mContext = context;
    }

    public void startListen() {
        this.mIntentFilter = new IntentFilter("android.intent.action.CLOSE_SYSTEM_DIALOGS");
        this.mHomeKeyBroadcastReceiver = new HomeKeyBroadcastReceiver();
        this.mContext.registerReceiver(this.mHomeKeyBroadcastReceiver, this.mIntentFilter);
        LogUtil.d(this.TAG, "-------->\u5f00\u59cb\u76d1\u542cHome\u952e");
    }

    public void stopListen() {
        if (this.mHomeKeyBroadcastReceiver != null) {
            this.mContext.unregisterReceiver(this.mHomeKeyBroadcastReceiver);
            LogUtil.d(this.TAG, "------->\u505c\u6b62\u76d1\u542cHome\u952e");
        }
    }

    public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) {
        this.mOnHomeKeyListener = homeKeyListener;
    }
}
